//
//  YXJCDetailVC.m
//  Tunnel
//
//  Created by wbb on 2025/6/14.
//

#import "JKGJDetailVC.h"
#import "TYBaseTableViewCell.h"
#import "YXJCDetailEChartsView.h"
#import "JKGJModel.h"
@interface JKGJDetailCell : TYBaseTableViewCell

@end
@implementation JKGJDetailCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self initUI];
    }
    return self;
}

- (void)initUI {
    UILabel *currentLab = nil;
    for (int i = 0; i<3; i++) {
        UILabel *headerLab = [[UILabel alloc] init];
        headerLab.text = @"测试";
        headerLab.tag = 10000000+i;
        headerLab.textColor = main_light_text_color_6;
        headerLab.textAlignment = NSTextAlignmentCenter;
        headerLab.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:headerLab];
        [headerLab mas_makeConstraints:^(MASConstraintMaker *make) {
            if (i == 0) {
                make.left.offset(0);
            }else {
                make.left.equalTo(currentLab.mas_right);
                make.width.equalTo(currentLab.mas_width);
            }
            make.top.bottom.offset(0);
            if (i == 2) {
                make.right.offset(0);
            }
        }];
        
        
        currentLab = headerLab;
    }
}
- (void)cellWithData:(id)data {
    [super cellWithData:data];
    
    UILabel *lab1 = (UILabel *)[self.contentView viewWithTag:10000000];
    UILabel *lab2 = (UILabel *)[self.contentView viewWithTag:10000001];
    UILabel *lab3 = (UILabel *)[self.contentView viewWithTag:10000002];
    
    
    
}
@end

@interface JKGJDetailVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UIImageView * lineImgView;
@property (nonatomic, strong) UITableView * tableView;
@property (nonatomic, copy) NSArray * dataArr;
@property (nonatomic, strong) UIView * bottomBgView;
@property (nonatomic, strong) UIView * headerView;
@property (nonatomic, strong) YXJCDetailEChartsView *chartView;

@property (nonatomic, assign) NSInteger  currentTag;
@property (nonatomic, strong) JKGJModel  * model;
@end

@implementation JKGJDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    kWEAK_SELF
    [TYNetWorkTool getRequest:[NSString stringWithFormat:@"%@%@",TYGetRulesalarmDetail,weakSelf.jkId] parameters:@{} successBlock:^(BOOL success, NSDictionary * _Nonnull data, NSString * _Nonnull msg) {
        weakSelf.model = [JKGJModel yy_modelWithJSON:data[@"data"]];
        [weakSelf initUI];
    } failureBlock:^(NSString * _Nonnull description) {
        
    }];
    
    
    self.title = @"告警详情";
    self.dataArr = @[@{
        @"time":@"1:00",
        @"xxx":@"12",
        @"status":@"正常"
    },@{
        @"time":@"1:00",
        @"xxx":@"12",
        @"status":@"正常"
    },@{
        @"time":@"1:00",
        @"xxx":@"12",
        @"status":@"正常"
    },@{
        @"time":@"1:00",
        @"xxx":@"12",
        @"status":@"正常"
    },@{
        @"time":@"1:00",
        @"xxx":@"12",
        @"status":@"正常"
    },@{
        @"time":@"1:00",
        @"xxx":@"12",
        @"status":@"正常"
    },@{
        @"time":@"1:00",
        @"xxx":@"12",
        @"status":@"正常"
    },@{
        @"time":@"1:00",
        @"xxx":@"12",
        @"status":@"正常"
    }];
 
}
- (void)initUI {
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, self.view.height)];
    [self.view addSubview:scrollView];
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    
    UIView *topBgView = [[UIView alloc] init];
    topBgView.backgroundColor = [UIColor whiteColor];
    [scrollView addSubview:topBgView];
    topBgView.layer.shadowColor = [UIColor colorWithRed:156/255.0 green:175/255.0 blue:210/255.0 alpha:0.2].CGColor;
    topBgView.layer.shadowOffset = CGSizeMake(0,4);
    topBgView.layer.shadowOpacity = 1;
    topBgView.layer.shadowRadius = 8;
    topBgView.layer.cornerRadius = 7;
    
    {
        UILabel  *titleLabel = [[UILabel alloc] init];
        titleLabel.font = [UIFont systemFontOfSize:17];
        titleLabel.textColor = main_text_color;
        titleLabel.text = @"基础信息";
        [topBgView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(16);
            make.top.offset(12);
        }];
        
        NSArray *arr = @[@"告警设备", @"告警类型",@"告警等级",@"告警位置",@"告警来源",@"告警时间"];
        NSArray * valueArr = @[[NSString stringWithFormat:@"%@-%@",self.model.devName?:@"",self.model.devId?:@""],self.model.type?:@"",self.model.grade?:@"",[NSString stringWithFormat:@"%@ %@",self.model.direction?:@"",self.model.pileNo?:@""],self.model.infoFrom?:@"",self.model.createTime?:@""];
        UIStackView *currentStackView = nil;
        for (int i = 0; i<arr.count; i++) {
            UIStackView *stackView1 = [[UIStackView alloc] init];
            stackView1.spacing = 10;
            [topBgView addSubview:stackView1];
            [stackView1 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.offset(15);
                if(i == 0){
                    make.top.equalTo(titleLabel.mas_bottom).offset(5);
                }else {
                    make.top.equalTo(currentStackView.mas_bottom);
                }
                make.height.offset(20);
            }];
            
            UILabel *leftLab1 = [[UILabel alloc] init];
            leftLab1.text = arr[i];
            leftLab1.textColor = main_light_text_color_9;
            leftLab1.font = [UIFont systemFontOfSize:14];
            
            UILabel * ssxtLab = [[UILabel alloc] init];
            ssxtLab.text = valueArr[i];
            ssxtLab.tag = i+1000;
            ssxtLab.textColor = main_light_text_color_6;
            ssxtLab.font = [UIFont systemFontOfSize:14];
            
            [stackView1 addArrangedSubview:leftLab1];
            [stackView1 addArrangedSubview:ssxtLab];
            
            currentStackView = stackView1;
        }
        
        
        
        [topBgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(5);
            make.left.offset(12);
            make.right.offset(-12);
            make.bottom.equalTo(currentStackView.mas_bottom).offset(5);
            make.width.equalTo(scrollView.mas_width).offset(-24);
        }];
    }
   
    
    
    UIView *topBgView1 = [[UIView alloc] init];
    topBgView1.backgroundColor = [UIColor whiteColor];
    [scrollView addSubview:topBgView1];
    topBgView1.layer.shadowColor = [UIColor colorWithRed:156/255.0 green:175/255.0 blue:210/255.0 alpha:0.2].CGColor;
    topBgView1.layer.shadowOffset = CGSizeMake(0,4);
    topBgView1.layer.shadowOpacity = 1;
    topBgView1.layer.shadowRadius = 8;
    topBgView1.layer.cornerRadius = 7;
    
    {
        UILabel  *titleLabel = [[UILabel alloc] init];
        titleLabel.font = [UIFont systemFontOfSize:17];
        titleLabel.textColor = main_text_color;
        titleLabel.text = @"告警内容";
        [topBgView1 addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(16);
            make.top.offset(12);
        }];
        NSString * jcz = @"";
        NSString * fzsx = @"";
        NSString * fzxx = @"";
        if(self.model.conditionValue){
          NSArray * conditArr = [self.model.conditionValue componentsSeparatedByString:@"/"];
            jcz = [NSString stringWithFormat:@"%@",conditArr.lastObject];
            fzsx = [NSString stringWithFormat:@"%@",conditArr[conditArr.count - 3]];
            fzxx = [NSString stringWithFormat:@"%@",conditArr[conditArr.count - 2]];
        }
        
        NSArray *arr = @[@"告警原因",@"阈值上限",@"阈值下限",@"监测值 "];
        NSArray *valArr = @[self.model.cause?:@"",fzsx?:@"",fzxx?:@"",jcz?:@""];
        UIStackView *currentStackView = nil;
        for (int i = 0; i<arr.count; i++) {
            UIStackView *stackView1 = [[UIStackView alloc] init];
            stackView1.spacing = 10;
            [topBgView1 addSubview:stackView1];
            [stackView1 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.offset(15);
                if(i == 0){
                    make.top.equalTo(titleLabel.mas_bottom).offset(5);
                }else {
                    make.top.equalTo(currentStackView.mas_bottom);
                }
                make.right.offset(-15);
            }];
            
            UILabel *leftLab1 = [[UILabel alloc] init];
            leftLab1.text = arr[i];
            leftLab1.textColor = main_light_text_color_9;
            leftLab1.font = [UIFont systemFontOfSize:14];
            
            UILabel * ssxtLab = [[UILabel alloc] init];
            ssxtLab.text = valArr[i];
            ssxtLab.numberOfLines = 0;
            ssxtLab.lineBreakMode = NSLineBreakByWordWrapping;
            ssxtLab.tag = i+1100;
            ssxtLab.textColor = main_light_text_color_6;
            ssxtLab.font = [UIFont systemFontOfSize:14];
            
            [stackView1 addArrangedSubview:leftLab1];
            [stackView1 addArrangedSubview:ssxtLab];
            stackView1.alignment = UIStackViewAlignmentTop;
            stackView1.distribution = UIStackViewDistributionFill; // 填充可用空间
            [leftLab1 setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
            [ssxtLab setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
            stackView1.axis = UILayoutConstraintAxisHorizontal;
            
            currentStackView = stackView1;
        }
        
        [topBgView1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(topBgView.mas_bottom).offset(15);
            make.right.offset(-12);
            make.bottom.equalTo(currentStackView.mas_bottom).offset(5);
            make.width.equalTo(scrollView.mas_width).offset(-24);
        }];
    }
    
    UIView *topBgView2 = [[TYBoxView alloc] init];
    [scrollView addSubview:topBgView2];

    
    {
        UILabel  *titleLabel = [[UILabel alloc] init];
        titleLabel.font = [UIFont systemFontOfSize:17];
        titleLabel.textColor = main_text_color;
        titleLabel.text = @"处置建议";
        [topBgView2 addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(16);
            make.top.offset(12);
        }];
        
        UILabel * czjyLab = [[UILabel alloc] init];
        czjyLab.text = self.model.condition;
        czjyLab.tag = 1200;
        czjyLab.numberOfLines = 0;
        czjyLab.textColor = main_light_text_color_6;
        czjyLab.font = [UIFont systemFontOfSize:14];
        [topBgView2 addSubview:czjyLab];
        
        [czjyLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(titleLabel.mas_left);
            make.top.equalTo(titleLabel.mas_bottom).offset(10);
            make.right.offset(-12);
        }];
        
        [topBgView2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(topBgView1.mas_bottom).offset(15);
            make.left.offset(12);
            make.right.offset(-12);
            make.bottom.equalTo(czjyLab.mas_bottom).offset(10);
            make.width.equalTo(scrollView.mas_width).offset(-24);
            make.bottom.equalTo(scrollView.mas_bottom);
        }];
    }
    
    [scrollView layoutIfNeeded];
    
}

- (void)onType:(UIButton *)btn {
    NSInteger tag = btn.tag;
    
    
    if(self.currentTag == tag) {
        return;
    }
    if(tag == 10){// select1
        
    }else if (tag == 11){ // select2
        
    }else if (tag == 12){// tab1
        
        self.tableView.hidden = NO;
        self.chartView.hidden = YES;
        [self.tableView  mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.headerView.mas_bottom);
            make.left.right.offset(0);
            make.height.offset(self.dataArr.count *24);
            make.bottom.offset(0);
        }];
        [self.chartView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.offset(0);
            make.height.offset(0);
        }];
        
        [self.lineImgView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(btn.mas_bottom).offset(0);
            make.centerX.equalTo(btn.mas_centerX);
            make.width.offset(20);
            make.height.offset(4);
        }];
    }else if (tag == 13){// tab2
        self.tableView.hidden = YES;
        self.chartView.hidden = NO;
        
        [self.chartView viewWithData:@[]];
        [self.tableView  mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.offset(0);
            make.height.offset(0);
        }];
        [self.chartView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.headerView.mas_bottom);
            make.left.right.offset(0);
            make.height.offset(178);
            make.bottom.offset(0);
        }];
        
        [self.lineImgView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(btn.mas_bottom).offset(0);
            make.centerX.equalTo(btn.mas_centerX);
            make.width.offset(20);
            make.height.offset(4);
        }];
    }
    
    self.currentTag = tag;
}
#pragma mark ---- UITableViewDelegate, UITableViewDataSource ----
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 24;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    JKGJDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[JKGJDetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    [cell cellWithData:self.dataArr[indexPath.row]];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}
#pragma mark ---- lazy ----
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.scrollEnabled = NO;
    }
    return _tableView;
}
@end
